package com.bjqh.edu.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Index;
import com.bjqh.edu.Bean.User;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.Index.FreeReservationActivity;
import com.bjqh.edu.UI.Activity.Index.OpenClassActivity;
import com.bjqh.edu.UI.Activity.Index.PrivateClassActivity;
import com.bjqh.edu.UI.Activity.Index.WebViewAcitviy;
import com.bjqh.edu.UI.Activity.StudentPersonal.HistoricalGradeActivity;
import com.bjqh.edu.UI.Activity.Index.ScheduleActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.CoursePurchaseActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.MyTeacherActivity;
import com.bjqh.edu.UI.Utils.ComnUtils;
import com.bjqh.edu.UI.Utils.SharedPrefsUtil;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/9/22.
 */

public class IndexMenuAdapter extends BaseAdapter {

    private List<Index.MenuBean> list;
    private Context ctx;
    private ViewHolder holder;

    public IndexMenuAdapter(Context ctx, List<Index.MenuBean> data) {
        this.ctx = ctx;
        this.list = data;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(ctx).inflate(R.layout.item_index_menu, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.tvMenu.setText(list.get(i).getName());
        Glide.with(ctx)
                .load(AppConstants.IMG_URL + list.get(i).getImg())
                .asBitmap()
                .placeholder(R.mipmap.iv_err)
                .into(holder.cvMe);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //免费试听课
                if (list.get(i).getName().equals("课程预约")) {
                    Intent intent = new Intent();
                    intent.putExtra("url", AppConstants.H5_URL + "reg.html");
                    intent.setClass(ctx, FreeReservationActivity.class);
                    ctx.startActivity(intent);
                }

                //课程购买
                else if (list.get(i).getName().equals("购买课程")) {
                    ctx.startActivity(new Intent(ctx, CoursePurchaseActivity.class));
                }
                //我的老师
                else if (list.get(i).getName().equals("我的老师")) {
                    ctx.startActivity(new Intent(ctx, MyTeacherActivity.class));
                }
                //历史成绩
                else if (list.get(i).getName().equals("历史成绩")) {
                    ctx.startActivity(new Intent(ctx, HistoricalGradeActivity.class));
                } else if (list.get(i).getName().equals("课程表")) {
                    ctx.startActivity(new Intent(ctx, ScheduleActivity.class));
                } else if (!(TextUtils.isEmpty(list.get(i).getName()) && TextUtils.isEmpty(list.get(i).getUrl()))) {
                    if (1 == list.get(i).getLogin()) {
                        OkGo.post(AppConstants.BASE_URL + "/user/userInfor.shtml")
                                .tag(this)
                                .cacheKey("cacheKey")
                                .cacheMode(CacheMode.DEFAULT)
                                .execute(new StringCallback() {
                                    @Override
                                    public void onSuccess(String s, okhttp3.Call call, okhttp3.Response response) {
                                        Log.e("user", s);
                                        Gson gson = new Gson();
                                        User user = gson.fromJson(s, User.class);
                                        if (user.getMsg().equals("success")) {
                                            User.UserBean userBean = user.getUser();
                                            if (userBean != null) {
                                                Map<String, Object> map = new HashMap<String, Object>();
                                                map.put("qianhexuetang", "qianhexuetang");
                                                map.put("phonenumber", user.getUser().getMobile());
                                                map.put("num", user.getUser().getId());
                                                String url = ComnUtils.getInstanceOfComnUtils(ctx).getUrl(map, list.get(i).getUrl());
                                                Intent intent = new Intent(ctx, WebViewAcitviy.class);
                                                intent.putExtra("url", url);
                                                ctx.startActivity(intent);
                                            }


                                        } else if (user.getMsg().equals("timeout")) {
                                            AppConstants.gotoLogin(ctx);
                                        } else if (user.getMsg().equals("remote login")) {
                                            AppConstants.remologin(ctx);
                                        }


                                    }

                                    @Override
                                    public void onError(Call call, Response response, Exception e) {
                                        Toast.makeText(ctx, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                                    }
                                });

                    } else {
                        Intent intent = new Intent(ctx, WebViewAcitviy.class);
                        intent.putExtra("url", list.get(i).getUrl());
                        ctx.startActivity(intent);

                    }


                }


            }
        });
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.cv_me)
        ImageView cvMe;
        @BindView(R.id.tv_menu)
        TextView tvMenu;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
